﻿using System;
using System.Collections.Generic;
using System.Linq;
using Google.GData.Client;
using Google.YouTube;
using System.Configuration;
using System.Web;

namespace MVC3DEMO.Models.Youtube
{
    public class YouTubeVideoObject
    {
        public string VideoId { get; set; }
        public string Title { get; set; }
    }

    public class YouTubeVideoHelper
    {
        const string YOUTUBE_CHANNEL = "thanhluansoft";
        /*AI39si58Nz1BfWygZ3l5f1FcJUSU5B3LBW_luLry8fxwsmBurK-bCPuXNRolDpXY_PPMqNEBV7nmH3knZyujjHPLAZ6ITfuZaw------AI39si6JqO_f2b_JWSV3QUbcwg5S-1RJ4-kiieosBZy9r1ORkCAv7BT5tLp579Tzmly8rvOVm3Jyueq3ZVqUNt1blS4DcoVppA*/
        const string YOUTUBE_DEVELOPER_KEY = "AI39si58Nz1BfWygZ3l5f1FcJUSU5B3LBW_luLry8fxwsmBurK-bCPuXNRolDpXY_PPMqNEBV7nmH3knZyujjHPLAZ6ITfuZaw";
        public static YouTubeRequest GetRequest()
        {
            var request = HttpContext.Current.Session["YTRequest"] as YouTubeRequest;

            if (request == null)
            {

                var settings = new YouTubeRequestSettings("Video",YOUTUBE_DEVELOPER_KEY,"thanhluan.soft","longthanhcamgiadongchaythoigian");

                request = new YouTubeRequest(settings);

                HttpContext.Current.Session["YTRequest"] = request;

            }

            return request;
            

           
        }
        public static YouTubeVideoObject[] GetVideos()
        {

            YouTubeRequestSettings settings = new YouTubeRequestSettings("Thanh Luan", YOUTUBE_DEVELOPER_KEY);
            YouTubeRequest request = new YouTubeRequest(settings);

            string feedUrl = String.Format("http://gdata.youtube.com/feeds/api/users/thanhluansoft/uploads", YOUTUBE_CHANNEL);
            Feed<Video> videoFeed = request.Get<Video>(new Uri(feedUrl));

            return (from video in videoFeed.Entries
                    select new YouTubeVideoObject() { VideoId = video.VideoId, Title = video.Title }).ToArray();
        }

    }
}